package com.endpoint.book.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.endpoint.book.entity.BookIndex;
import com.endpoint.book.service.BookIndexService;
import com.endpoint.book.to.BookIndexTo;
import com.endpoint.common.utils.ResultBean;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Project: endpoint
 * @Name: BookIndexController
 * @Author: Jack_Li
 * @Date: 2024/7/7 14:38
 */
@Slf4j
@RestController
@RequestMapping("/book/bookIndex")
public class BookIndexController {

    @Autowired
    private BookIndexService bookIndexService;

    @GetMapping("/chapterList")
    public ResultBean getChapterList(@RequestParam("bookId") Long bookId) {
        return bookIndexService.getChapterList(bookId);
    }

    @GetMapping("/list")
    public ResultBean<Page<BookIndexTo>> getBookIndexByBookId(@RequestParam("bookId") Long bookId,
                                                              @RequestParam("pageNum") int pageNum,
                                                              @RequestParam("pageSize") int pageSize) {
        return bookIndexService.getChapterPage(bookId, pageNum, pageSize);
    }

    //根据bookId查询 小说最后更新章节信息
    @GetMapping("/getlastChapterByBookId")
    public ResultBean<BookIndexTo> getlastChapterByBookId (@RequestParam("bookId")Long bookId){
        BookIndex bookIndex = bookIndexService.getOne(new QueryWrapper<BookIndex>()
                .eq("book_Id", bookId)
                .orderByDesc("index_num")
                .last("limit 1"));
        BookIndexTo bookIndexTo = new BookIndexTo();
        if(bookIndex != null) {
            BeanUtils.copyProperties(bookIndex,bookIndexTo);
        }
        return ResultBean.ok(bookIndexTo);
    }

    //保存发布章节
    @PostMapping("/saveChapter")
    public ResultBean<BookIndexTo>saveChapter(@RequestBody BookIndexTo bookIndex){
        BookIndex data = new BookIndex();
        log.warn("bookIndex:{}", bookIndex);
        BeanUtils.copyProperties(bookIndex,data);
        log.warn("data:{}", data);
        data.setCreateTime(new Date());
        data.setUpdateTime(new Date());
        bookIndexService.save(data);
        bookIndex.setId(data.getId());
        return ResultBean.ok(bookIndex);
    }

    //查询小说信息
    @ApiOperation(value = "查询小说信息")
    @GetMapping("/getBookIndexById/{id}")
    public ResultBean<BookIndex> getBookIndexById(@PathVariable("id") Long id){
        BookIndex bookIndex = bookIndexService.getById(id);
        return ResultBean.ok(bookIndex);
    }

    //查询小说第一章节
    @ApiOperation(value = "查询小说第一章节")
    @GetMapping("/getfirstChapterByBookId")
    public ResultBean<BookIndex> getfirstChapterByBookId (@RequestParam("bookId")Long bookId){
        BookIndex bookIndex = bookIndexService.getOne(new QueryWrapper<BookIndex>()
                .eq("book_Id", bookId)
                .orderByAsc("index_num")
                .last("limit 1"));
        return ResultBean.ok(bookIndex);
    }

}